#!/bin/bash

MK_GENERATED_IMGS_PATH=generated

rm -fr wm_tmp
mkdir -p wm_tmp

if [ "${CDK_VERSION}" != '' ];then
echo ${CDK_VERSION}
#Parse from yaml
PRODUCT_BIN=${BOARD_PATH}/configs/product.exe
ELF_NAME=`ls Obj/*.elf`
WM_TOOL=${BOARD_PATH}/configs/wm_tool.exe

#Dir prepare
rm -fr ${MK_GENERATED_IMGS_PATH}
mkdir -p ${MK_GENERATED_IMGS_PATH}
mkdir -p ${MK_GENERATED_IMGS_PATH}/data

cp ${BOARD_PATH}/configs/config.yaml ${MK_GENERATED_IMGS_PATH}/data/
#boot
if [ -f "${BOARD_PATH}/bootimgs/boot" ]; then cp ${BOARD_PATH}/bootimgs/boot ${MK_GENERATED_IMGS_PATH}/data/; fi
#BT
if [ -f "${BOARD_PATH}/bootimgs/bt" ]; then cp ${BOARD_PATH}/bootimgs/bt ${MK_GENERATED_IMGS_PATH}/data/; fi
if [ -f "${BOARD_PATH}/bootimgs/fcds" ]; then cp ${BOARD_PATH}/bootimgs/fcds ${MK_GENERATED_IMGS_PATH}/data/; fi
else
PRODUCT_BIN=./script/product
chmod +x ${PRODUCT_BIN}
gcc ../../components/chip_w800/wmsdk/tools/w800/wm_tool.c -Wall -lpthread -O2 -o ./wm_tmp/wm_tool
WM_TOOL=./wm_tmp/wm_tool
chmod +x ${WM_TOOL}
ELF_NAME=yoc.elf
fi

#Tools bin
OBJCOPY=csky-elfabiv2-objcopy

echo "[INFO] Generated output files ..."

${OBJCOPY} -O binary ${ELF_NAME} ${MK_GENERATED_IMGS_PATH}/data/prim

${PRODUCT_BIN} image ${MK_GENERATED_IMGS_PATH}/images.zip -i ${MK_GENERATED_IMGS_PATH}/data -l -v "1.0.0" -p
${PRODUCT_BIN} image ${MK_GENERATED_IMGS_PATH}/images.zip -e ${MK_GENERATED_IMGS_PATH} -x
${PRODUCT_BIN} image ${MK_GENERATED_IMGS_PATH}/images.zip -e ${MK_GENERATED_IMGS_PATH} -tb

echo "Create wm bin files"

cp ${MK_GENERATED_IMGS_PATH}/data/boot wm_tmp/boot.bin
cp ${MK_GENERATED_IMGS_PATH}/data/prim wm_tmp/yoc.bin
cp ${MK_GENERATED_IMGS_PATH}/data/prim wm_tmp/prim.bin
cp ${MK_GENERATED_IMGS_PATH}/imtb wm_tmp/imtb.bin


${WM_TOOL} -b ./wm_tmp/yoc.bin -fc 0 -it 1 -ih 8132C00 -ra 8013000 -ua 8133000 -nh 0 -un 0 -vs yoc-7.3.0
#cat ../../components/chip_w800/wmsdk/tools/w800/w800_secboot.img ./wm_tmp/yoc.img > yoc.fls
${WM_TOOL} -b ./wm_tmp/yoc.img -fc 1 -it 1 -ih 8132C00 -ra 8013000 -ua 8133000 -nh 0 -un 0 -vs yoc-7.3.0
mv ./wm_tmp/yoc_gz.img yoc_ota.bin

echo [INFO] Create fls file

${WM_TOOL} -b ./wm_tmp/boot.bin -fc 0 -it 512 -ih 8002000 -ra 8002400 -ua 8133000 -nh 8132C00 -un 0
${WM_TOOL} -b ./wm_tmp/prim.bin -fc 0 -it 1 -ih 8132C00 -ra 8013000 -ua 8133000 -nh 0 -un 0
${WM_TOOL} -b ./wm_tmp/imtb.bin -fc 0 -it 1 -ih 20047400 -ra 800F000 -ua 0 -nh 0 -un 0
cat ./wm_tmp/boot.img ./wm_tmp/prim.img ./wm_tmp/imtb.img > yoc_total.fls
rm -fr wm_tmp
if [ -f "${BOARD_PATH}/bootimgs/bt" ]; then cat yoc_ota.bin ${MK_GENERATED_IMGS_PATH}/data/bt > total_fota.bin
else
cp yoc_ota.bin total_fota.bin
fi

